class Handler {
    constructor() {
        this.nextHandler = null
    }
    setNextHandler (handler) {
        this.nextHandler = handler
    }
    handleRequest (request) {
        if (this.nextHandler !== null) {
            return this.nextHandler.handleRequest(request)
        }
        return null
    }
}

class ConcreteHandlerA extends Handler {
    handleRequest (request) {
        if (request === 'A') {
            return 'handled ' + request
        }
        return super.handleRequest(request)
    }
}

class ConcreteHandlerB extends Handler {
    handleRequest (request) {
        if (request === 'B') {
            return 'handled ' + request
        }
        return super.handleRequest(request)
    }
}

class ConcreteHandlerC extends Handler {
    handleRequest (request) {
        if (request === 'C') {
            return 'handled ' + request
        }
        return super.handleRequest(request)
    }
}

const handlerA = new ConcreteHandlerA()
const handlerB = new ConcreteHandlerB()
const handlerC = new ConcreteHandlerC()

handlerA.setNextHandler(handlerB)
handlerB.setNextHandler(handlerC)

console.log(handlerA.handleRequest('A'))
console.log(handlerA.handleRequest('B'))
console.log(handlerA.handleRequest('C'))
console.log(handlerA.handleRequest('D'))